package com.duan.laugh.activiti.controller;

import cn.hutool.core.io.IoUtil;
import com.duan.laugh.activiti.service.ActTaskService;
import com.duan.laugh.common.core.util.R;
import lombok.AllArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.InputStream;
import java.util.Map;

/**
 * @author duanjw
 */
@RestController
@AllArgsConstructor
@RequestMapping("/task")
public class TaskController {
    private final ActTaskService actTaskService;

    @GetMapping("/todo")
    public R todo(@RequestParam Map<String, Object> params) {
        return R.ok(actTaskService.getTaskByName(params, ""));
    }

//	@GetMapping("/{id}")
//	public R getTaskById(@PathVariable String id) {
//		return R.ok(actTaskService.getTaskById(id));
//	}

    /**
     * 根据任务id完成任务
     *
     * @param id
     * @param map
     * @return
     */
    @PutMapping("/complete/{id}")
    public R complete(@PathVariable String id, @RequestBody Map map) {
        return R.ok(actTaskService.complete(id, map));
    }

    /**
     * 根据任务id查看流程图
     * <p>
     * 红色圈表示当前状态
     *
     * @param id
     * @return
     */
    @GetMapping("/view/{id}")
    public ResponseEntity viewCurrentImage(@PathVariable String id) {
        InputStream imageStream = actTaskService.viewByTaskId(id);
        return new ResponseEntity(IoUtil.readBytes(imageStream), HttpStatus.CREATED);
    }

    @GetMapping("/comment/{id}")
    public R commitList(@PathVariable String id) {
        return R.ok(actTaskService.getCommentByTaskId(id));
    }


}
